package com.comleader.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.comleader.entity.hopper.Global;
import com.comleader.entity.hopper.Hopper;
import com.comleader.utils.HopperUtils;
import com.comleader.utils.ResponseUtil;
import com.comleader.utils.TimeUtils;
import com.graphhopper.PathWrapper;
import com.graphhopper.util.GPXEntry;
import com.graphhopper.util.shapes.GHPoint;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * Hello world!
 *
 */
@RestController
@RequestMapping("/route")
public class PlanController {
    @PostMapping(value = "/plan")
    public Map get(@RequestBody String plist,Hopper hopper){
        System.out.println(new Date());
        HashMap<Object, Object> map = new HashMap<>();
        String ghlist = plist.replace("\"", "").replace("\"", "");
        String s = HopperUtils.Gaodepoints(ghlist);
        List<GHPoint> ghPoints= JSONArray.parseArray(s,GHPoint.class);
        PathWrapper path = null;
        try {
            hopper.setVehicle(Global.Vehicle.SIX);
            hopper.setWeighting(Global.Weighting.ZERO);

            path = HopperUtils.path(ghPoints, hopper);
            // 或获取json
            //路线信息
            List<Map<String, Object>> iList = path.getInstructions().createJson();
            // 或以gpx条目的形式获取结果：
            List<GPXEntry> list = path.getInstructions().createGPXList();
            //路线规划坐标点集合
            List list1 = HopperUtils.GaodegpxList(list);
            //途径点经纬度集合
            map.put("gpx",list1);
            System.out.println(list1);
//            for (Object o : list1) {
//                String[] split = String.valueOf(o).split(",");
//                for (String s1 : split) {
//                    System.out.println(s1);
//                }
//            }
            System.out.println(iList);
            //途径街道及转弯信息
            map.put("path",iList);
            //预计耗时
            map.put("time",TimeUtils.getGapTime(path.getTime()));
            System.out.println(new Date());
        } catch (Exception e) {
            map.put("msg","请到城市或者公路上使用此功能");
            System.err.println("没有路，路线规划为空");
        }
        return map;
    }
}
